﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using BI.ViewModels;
using Common;
using Models;
using System.IO;
using WindowManagerService;
using BI;

namespace THMClient
{
    public partial class THM_Table : Form
    {
        private THM_TableViewModel THM_TableViewModel = null;
        private Timer tableTimer = null;
        private BaseStrategy basestrategy = null;

        public THM_Table(THM_TableViewModel THM_TableViewModel)
        {
            InitializeComponent();
            basestrategy = new BaseStrategy();
            this.FormClosing += new FormClosingEventHandler(THM_Table_FormClosing);
            this.THM_TableViewModel = THM_TableViewModel;
            _pokerTable_groupbox.Text += string.Concat(": ", THM_TableViewModel.PokerWindow.Title);
            tableTimer = new Timer();
            tableTimer.Interval = 5000;
            tableTimer.Tick += new EventHandler(tableTimer_Tick);
            _wainting_label.Visible = false;
            _stop_scanning_button.Enabled = false;
            CleanTableGroup();
        }

        void THM_Table_FormClosing(object sender, FormClosingEventArgs e)
        {
            tableTimer.Stop();
        }

        void tableTimer_Tick(object sender, EventArgs e)
        {
            string directoryPath = THM_TableViewModel.SaveScreenOfTableWindow();
            PokerTable pokerTable = THM_TableViewModel.GetPokerTable(Path.Combine(Configuration.WindowScreenPath, directoryPath));
            if (pokerTable.IsMyTurn)
            {
                CleanTableGroup();
                SetUpTableGroup(pokerTable);
            }
            else
            {
                pokerTable.TableName = THM_TableViewModel.PokerWindow.Title;
                _wainting_label.Visible = true;
            }
        }

        private void SetUpTableGroup(PokerTable pokerTable)
        {
            pokerTable.TableName = THM_TableViewModel.PokerWindow.Title;

            if (pokerTable.TableActionButtons.FoldButtonEnabled)
            {
                _fold_button.Enabled = true;
            }
            if (pokerTable.TableActionButtons.CheckButtonEnabled)
            {
                _check_button.Enabled = true;
            }
            if (pokerTable.TableActionButtons.CallButtonEnabled)
            {
                _call_button.Enabled = true;
                _call_button.Text += string.Format(" ({0}$)",pokerTable.TableActionButtons.CallButtonChips);
            }
            if (pokerTable.TableActionButtons.BetButtonEnabled)
            {
                _bet_button.Enabled = true;
            }
            if (pokerTable.TableActionButtons.RaiseButtonEnabled)
            {
                _raise_button.Enabled = true;
            }
            if (pokerTable.Pot != 0)
            {
                _pot_label.Text = string.Format("{0}$", pokerTable.Pot);
            }

            _table_state.Text = pokerTable.TableState.ToString();
            _players_count.Text = pokerTable.OpponentsCount.ToString();
            _multiOddsCalcuator.Opponents = (pokerTable.OpponentsCount - 1);

            _first_hole_card.Text = pokerTable.HoleCards.FirstHoleCard.GetCardMark();
            _second_hole_card.Text = pokerTable.HoleCards.SecondHoleCard.GetCardMark();
            _multiOddsCalcuator.Pocket = string.Concat(pokerTable.HoleCards.FirstHoleCard.GetCardMark(), pokerTable.HoleCards.SecondHoleCard.GetCardMark());

            if (pokerTable.TableState == TableState.PostFlop || pokerTable.TableState == TableState.Turn || pokerTable.TableState == TableState.River)
            {
                _flop_first_card.Text = pokerTable.BoardCards.FlopCards.FirstFlopCard.GetCardMark();
                _flop_second_card.Text = pokerTable.BoardCards.FlopCards.SecondFlopCard.GetCardMark();
                _flop_third_card.Text = pokerTable.BoardCards.FlopCards.ThirdFlopCard.GetCardMark();
                _multiOddsCalcuator.Board = string.Concat(pokerTable.BoardCards.FlopCards.FirstFlopCard.GetCardMark(),
                                                          pokerTable.BoardCards.FlopCards.SecondFlopCard.GetCardMark(),
                                                          pokerTable.BoardCards.FlopCards.ThirdFlopCard.GetCardMark());

            }
            if (pokerTable.TableState == TableState.Turn || pokerTable.TableState == TableState.River)
            {
                _turn_card.Text = pokerTable.BoardCards.TurnCard.GetCardMark();
                _multiOddsCalcuator.Board += pokerTable.BoardCards.TurnCard.GetCardMark();
            }
            if (pokerTable.TableState == TableState.River)
            {
                _river_card.Text = pokerTable.BoardCards.RiverCard.GetCardMark();
                _multiOddsCalcuator.Board += pokerTable.BoardCards.RiverCard.GetCardMark();
            }
            _multiOddsCalcuator.UpdateContents();
            pokerTable.HeroWinRate = _multiOddsCalcuator.Playerwins;
            pokerTable.VilliansWinRate = _multiOddsCalcuator.Opponentwins;

            basestrategy.MakeDecision(pokerTable);
            _last_action.Text = pokerTable.Action.ToString();
            THM_TableViewModel.SendResult(pokerTable);
        }

        private void CleanTableGroup()
        {
            _fold_button.Enabled = false;
            _check_button.Enabled = false;
            _call_button.Enabled = false;
            _call_button.Text = "Call";
            _bet_button.Enabled = false;
            _raise_button.Enabled = false;
            _wainting_label.Visible = false;
            _first_hole_card.Text = string.Empty;
            _second_hole_card.Text = string.Empty;
            _table_state.Text = string.Empty;
            _players_count.Text = string.Empty;
            _flop_first_card.Text = string.Empty;
            _flop_second_card.Text = string.Empty;
            _flop_third_card.Text = string.Empty;
            _turn_card.Text = string.Empty;
            _river_card.Text = string.Empty;
            _last_action.Text = string.Empty;
            _multiOddsCalcuator.Clear();
        }

        private void _get_pokerTable_from_image_Click(object sender, EventArgs e)
        {
            string path = @"D:\";
            PokerTable pokerTable = THM_TableViewModel.GetPokerTable(path);
        }

        private void _start_scan_Click(object sender, EventArgs e)
        {
            tableTimer.Start();
            _wainting_label.Visible = true;
            _start_scan.Enabled = false;
            _stop_scanning_button.Enabled = true;
        }

        private void _fold_button_Click(object sender, EventArgs e)
        {
            THM_TableViewModel.SendFoldCallKey();
        }

        private void _check_button_Click(object sender, EventArgs e)
        {
            THM_TableViewModel.SendCheckCallKey();
        }

        private void _call_button_Click(object sender, EventArgs e)
        {
            THM_TableViewModel.SendCheckCallKey();
        }

        private void _bet_button_Click(object sender, EventArgs e)
        {
            THM_TableViewModel.SendBetRaiseCallKey();
        }

        private void _raise_button_Click(object sender, EventArgs e)
        {
            THM_TableViewModel.SendBetRaiseCallKey();
        }

        private void _stop_scanning_button_Click(object sender, EventArgs e)
        {
            tableTimer.Stop();
            _start_scan.Enabled = true;
            _stop_scanning_button.Enabled = false;
            CleanTableGroup();
        }
    }
}
